<template>
	<view class="mySettle">
		<label class="radio">
			<radio value="" :checked="allchecked" @click="handleClick" /><text>全选</text>
		</label>
		<view class="pricetxt">
			合计: <text class="price">¥{{total}}</text>
		</view>
		<view class="btn-settle" @click="clickSettle">
			结算
		</view>
	</view>
</template>

<script>
	import {mapState,mapGetters,mapMutations} from 'vuex'
	export default {
		name:"my-settle",
		data() {
			return {
				
			};
		},
		methods:{
			handleClick(){
				this.$emit('changeAll')
			},
			// 点击结算按钮
			clickSettle(){
				console.log('this.cart',this.cart)
				// 判断用户有没勾选上商品
				let res = this.cart.every(v=>v.goods_state ===false || v.goods_state===2)
				if(res) return uni.$showMsg('请先勾选商品');
				// 判断用户有没有选择地址
				if(!this.addstr) return uni.$showMsg('请先选择收货地址');
				// 验证有没登录
				if(!this.token){
					uni.$showMsg('请先登录');
					setTimeout(()=>{
						uni.switchTab({
							url:'../../pages/my/my'
						})
						this.updataRedirectInfo({
							openType:'switchTab',
							from:'/pages/cart/cart'
						})
					},2000)
					return
				}
				
				// 当验证都通过时 调用创建订单方法
				this.payOrder();
			},
			// 获取请求体参数  并创建订单 
			async payOrder(){
				const orderInfo = {
					order_price:0.01,
					consignee_addr:this.addstr,
					goods:this.cart.filter(x=>x.goods_state).map(x=>({
						goods_id:x.goods_id,
						goods_number:x.goods_count,
						goods_price:x.goods_price
					}))
				}
					// 创建订单
				const { data: res } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				if (res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
				// console.log(res)
				const orderNumber = res.message.order_number;
				// console.log('orderNumber',orderNumber)
					// 预支付
				 const { data: res2 } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: orderNumber })
				if (res2.meta.status !== 200) return uni.$showMsg('预支付失败')
				console.log('支付',res2)
				const payInfo = res2.message.pay
				console.log('payInfo',payInfo)
				
				   //调用 uni.requestPayment() 发起微信支付------无法获取到
				   // let result = await uni.requestPayment(payInfo)
				   // console.log('result',result)
				   // const [err, succ] = await uni.requestPayment(payInfo)
				   // if (err) return uni.$showMsg('订单未支付！')
				   // console.log('succ=>',succ,'err=>',err)
				   // 完成了支付，进一步查询支付的结果
				   // const { data: res3 } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
				   // 检测到订单未支付
				   // if (res3.meta.status !== 200) return uni.$showMsg('订单未支付！')
				   // 检测到订单支付完成
				   // uni.showToast({
				   //   title: '支付完成！',
				   //   icon: 'success'
				   // })
			},
			
			...mapMutations('m_user',['updataRedirectInfo'])
		},
		props:{
			allchecked:{
				type:Boolean,
				default:false
			},
			total:{
				type:Number,
				default:0
			}
		},
		computed:{
			...mapState('m_cart',['cart']),
			...mapGetters('m_user',['addstr']),
			...mapState('m_user',['token'])
		}
	}
</script>

<style lang="scss">
.mySettle{
	display: flex;
	align-items: center;
	.radio{
		flex: 1;
		padding-left: 20rpx;
		box-sizing: border-box;
	}
	.pricetxt{
		flex:1;
		.price{
			color: #e4393c;
			margin-left: 5rpx;
			
		}
	}
	.btn-settle{
		flex: 1;
		height: 100%;
		line-height: 120rpx;
		text-align: center;
		color: #FFFFFF;
		background-color: #4cc6aa;
	}
}
	
	
.mySettle{
	width: 100%;
	height: 120rpx;
	position: fixed;
	bottom: 0;
	left: 0;
	z-index: 999;
	background-color: #FFFFFF;
	border-top: 2rpx solid #c7c7c7;
}
</style>
